package mundo.java.client.view;

import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Window;
import com.google.inject.Inject;

import mundo.java.client.Artigo.Messages;
import mundo.java.client.events.FailureEvent;
import mundo.java.client.events.GreetingResponseEvent;
import mundo.java.client.events.Event.Handler;
import mundo.java.shared.GreetingAction;
import mundo.java.shared.GreetingResponse;

public class ArtigoAlertView extends ArtigoAbstractView {
	@Inject
	public ArtigoAlertView(HandlerManager eventBus, final Messages messages) {
		super(eventBus, messages);
		
		eventBus.addHandler(FailureEvent.TYPE, new Handler<FailureEvent>() {
			@Override
			public void onEvent(FailureEvent event) {
				Window.alert(messages.rpcFailure() + "\n\n" + messages.serverError());
			}
		});
		
		eventBus.addHandler(GreetingResponseEvent.TYPE, new Handler<GreetingResponseEvent>() {
			@Override
			public void onEvent(GreetingResponseEvent event) {
				GreetingAction action = event.action;
				GreetingResponse response = event.response;
				Window.alert(greet(action.getTextToServer(), response.getServerInfo(), response.getUserAgent()));
			}
		});
	}

	@Override
	public String greet(String text, String serverInfo, String userAgent) {
		return messages.greeting(text) + "!\n\n" +
			messages.serverInfoGreeting(serverInfo) + "\n\n" +
			messages.userAgentGreeting() + "\n" + userAgent;
	}
}